package com.sinping.qh.service.app.records.impl;

import com.sinping.qh.api.exception.HandlerException;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.domain.common.UploadImage;
import com.sinping.qh.domain.records.CheckRecord;
import com.sinping.qh.domain.records.HighLightRecord;
import com.sinping.qh.dto.app.records.HighLightByAppDTO;
import com.sinping.qh.dto.common.UploadImageDTO;
import com.sinping.qh.repository.mongo.common.UploadImageRepository;
import com.sinping.qh.repository.mongo.records.CheckRecordsRepository;
import com.sinping.qh.repository.mongo.records.HighLightRepository;
import com.sinping.qh.service.app.records.IHighLightService;
import com.sinping.qh.service.common.CommonDbConst;
import com.sinping.qh.utils.mapper.BeanMapper;
import com.sinping.qh.utils.misc.IdGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangjin
 * @create 2018/6/2
 */
@Service
public class HighLightServiceImpl implements IHighLightService{

    @Autowired
    HighLightRepository highLightRepository;

    @Autowired
    CheckRecordsRepository checkRecordsRepository;

    @Autowired
    private UploadImageRepository imageRepository;

    @Value(value = "${qh.upload.urlPath}")
    private String urlPath;

    /**
     * 添加亮点时注意,一条检查只能有一条亮点
     * @param highLightByAppDTO
     * @return
     */
    @Override
    public HighLightByAppDTO save(HighLightByAppDTO highLightByAppDTO) {

        HighLightRecord highLightRecord= BeanMapper.map(highLightByAppDTO,HighLightRecord.class);

        CheckRecord checkRecord=
                checkRecordsRepository.findOne(highLightRecord.getCheckId());
        if(checkRecord==null)
        {
            throw new HandlerException(ConstEum.FAIL,"找不到检查记录");
        }
        //检查状态不等于检查中
        if(!(checkRecord.getCheckStatus().equals(CommonDbConst.CHECKRECORD_STATUS_CHECKING)))
        {
            throw new HandlerException(ConstEum.FAIL,"检查记录已提交审核或已经审核");
        }
        HighLightRecord one =highLightRepository.findHighLightRecordByCheckId(highLightRecord.getCheckId());
        if(one !=null)
        {
            throw new HandlerException(ConstEum.FAIL,"已经存在亮点");
        }



        highLightRecord.setId(IdGenerator.uuid2().toString());

        highLightRecord.setUploadImageList(getUploadImage(highLightByAppDTO.getUploadImageList()));

        highLightRecord.setGmtCreated(new Date());
        highLightRecord.setGmtModified(new Date());

        highLightRecord=highLightRepository.save(highLightRecord);
        highLightByAppDTO =BeanMapper.map(highLightRecord,HighLightByAppDTO.class);
        return highLightByAppDTO;
    }

    @Override
    public FrontPage findPage(FrontPage frontPage) {
        return null;
    }

    @Override
    public HighLightByAppDTO findOne(String id) {
        HighLightRecord highLightRecord=highLightRepository.findOne(id);

        if(highLightRecord!=null && highLightRecord.getUploadImageList().size()>0)
        {
            List<UploadImage> uploadImageDTOList=
                    highLightRecord.getUploadImageList();

            for (UploadImage uploadImage:uploadImageDTOList)
            {
                uploadImage.setPath(urlPath+uploadImage.getPath());
            }
        }

        HighLightByAppDTO highLightByAppDTO =BeanMapper.map(highLightRecord,HighLightByAppDTO.class);
        return highLightByAppDTO;
    }


    /**
     * 一、删除隐患和亮点： 条件：检查记录状态为检查中 物理删除
     * 二、如果是检查组长，审核时也可以删除隐患和亮点
     * @param id
     */
    @Override
    public boolean delete(String id) {
        HighLightRecord highLightRecord=highLightRepository.findOne(id);
        CheckRecord checkRecord=checkRecordsRepository.findOne(highLightRecord.getCheckId());
        if(checkRecord.getCheckStatus().equals(CommonDbConst.CHECKRECORD_STATUS_CHECKING)) {
            highLightRepository.delete(id);
        }else
        {
            throw new HandlerException(ConstEum.FAIL,"检查记录待审核或已审核");
        }
        return true;
    }

    @Override
    public HighLightByAppDTO update(HighLightByAppDTO highLightByAppDTO) {
        HighLightRecord highLightRecord=highLightRepository.findOne(highLightByAppDTO.getId());

        if(highLightRecord==null)
        {
            throw new HandlerException(ConstEum.FAIL,"错误的亮点Id");
        }
        CheckRecord checkRecord=
                checkRecordsRepository.findOne(highLightRecord.getCheckId());
        if(checkRecord==null)
        {
            throw new HandlerException(ConstEum.FAIL,"找不到检查记录");
        }
        //检查状态不等于检查中
        if(!(checkRecord.getCheckStatus().equals(CommonDbConst.CHECKRECORD_STATUS_CHECKING)))
        {
            throw new HandlerException(ConstEum.FAIL,"检查记录已提交审核或已经审核");
        }
//        if(highLightByAppDTO.getUploadImageList()!=null&&highLightByAppDTO.getUploadImageList().size()>0) {
            //保存图片
            highLightRecord.setUploadImageList(getUploadImage(highLightByAppDTO.getUploadImageList()));
//        }
        //修改描述
//        if(StringUtils.isNotBlank(highLightByAppDTO.getDesc()))
//        {
            highLightRecord.setDesc(highLightByAppDTO.getDesc());
//        }
        //修改人
        if(StringUtils.isNotBlank(highLightByAppDTO.getUpdateUserId()))
        {
            highLightRecord.setUpdateUserId(highLightByAppDTO.getUpdateUserId());
        }
        highLightRecord.setGmtModified(new Date());

        highLightRecord =highLightRepository.save(highLightRecord);

        highLightByAppDTO=BeanMapper.map(highLightRecord,HighLightByAppDTO.class);

        return highLightByAppDTO;
    }


    private List<UploadImage> getUploadImage(List<UploadImageDTO> arrayList)
    {
        ArrayList<UploadImage> uploadImages = new ArrayList<UploadImage>();
        for(UploadImageDTO uploadImageDTO :arrayList) {
            if(uploadImageDTO!=null && StringUtils.isNotBlank(uploadImageDTO.getId())) {
                UploadImage uploadImage = imageRepository.findOne(uploadImageDTO.getId());
                if(uploadImage==null)
                {
                    throw new HandlerException(ConstEum.FAIL,"无效的图片");
                }
                uploadImages.add(uploadImage);
            }
        }
        return uploadImages;

    }
}
